<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div style="width: 100px;height: 100px;background-color: red;position: absolute;left: 0;top: 0;"></div>
    <script>
        var div = document.getElementsByTagName('div')[0];
        var disX,
            disY;
        // div上点击
        div.onmousedown = function (e) {
            disX = e.pageX - parseInt(div.style.left);
            disY = e.pageY - parseInt(div.style.top);
            // document上移动
            document.onmousemove = function (e) {
                // 事件对象
                var event = e || window.event;
                // 定位鼠标位置
                // console.log(e.pageX + " " + e.pageY);
                div.style.left = e.pageX - disX + "px";
                div.style.top = e.pageY - disY + "px";
            }
            // document上松开
            document.onmouseup = function () {
                // 解除document上的onmousemove事件
                document.onmousemove = null;
            }
        }

    </script>
</body>

</html>